<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Provides an array or document to serialize as BSON</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.mongodb-bson-serializable.html">« MongoDB\BSON\Serializable</a></li>
      <li style="float: right;"><a href="class.mongodb-bson-unserializable.html">MongoDB\BSON\Unserializable »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongodb-bson-serializable.html">MongoDB\BSON\Serializable</a></li>
    <li>Provides an array or document to serialize as BSON</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb-bson-serializable.bsonserialize" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\BSON\Serializable::bsonSerialize</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\BSON\Serializable::bsonSerialize</span> &mdash; <span class="dc-title">Provides an array or document to serialize as BSON</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-bson-serializable.bsonserialize-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\BSON\Serializable::bsonSerialize</strong></span>(): <span class="type"><span class="type">array</span>|<span class="type">object</span></span></div>

  <p class="para rdfs-comment">
   Called during serialization of the object to BSON. The method must return an
   <span class="type">array</span> or <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span>.
  </p>
  <p class="para">
   Root documents (e.g. a
   <span class="interfacename"><a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span> passed to
   <span class="function"><a href="function.mongodb.bson-fromphp.html" class="function">MongoDB\BSON\fromPHP()</a></span>) will always be serialized as a
   BSON document. For field values, associative arrays and
   <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span> instances will be serialized as a BSON
   document and sequential arrays (i.e. sequential, numeric indexes starting at
   <code class="literal">0</code>) will be serialized as a BSON array.
  </p>
  <p class="para">
   Users are encouraged to include an <span class="property">_id</span> property (e.g. a
   <span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span> initialized in your constructor)
   when returning data for a BSON root document; otherwise, the driver or
   database will need to generate a
   <span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span> when inserting or upserting the
   document, respectively.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-bson-serializable.bsonserialize-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb-bson-serializable.bsonserialize-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   An <span class="type">array</span> or <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span> to be serialized as
   a BSON array or document.
  </p>
 </div>



 <div class="refsect1 examples" id="refsect1-mongodb-bson-serializable.bsonserialize-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1397">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\BSON\Serializable::bsonSerialize()</strong></span> returning an associative array for root document</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyDocument&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">ObjectId</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$bson&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">fromPHP</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">MyDocument</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">toJSON</span><span style="color: #007700">(</span><span style="color: #0000BB">$bson</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
{ &quot;_id&quot; : { &quot;$oid&quot; : &quot;56cccdcada14d8755a58c591&quot; }, &quot;foo&quot; : &quot;bar&quot; }
</pre></div>
   </div>
  </div>

  <div class="example" id="example-1398">
   <p><strong>示例 #2 <span class="function"><strong>MongoDB\BSON\Serializable::bsonSerialize()</strong></span> returning a sequential array for root document</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyArray&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$bson&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">fromPHP</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">MyArray</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">toJSON</span><span style="color: #007700">(</span><span style="color: #0000BB">$bson</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
{ &quot;0&quot; : 1, &quot;1&quot; : 2, &quot;2&quot; : 3 }
</pre></div>
   </div>
  </div>

  <div class="example" id="example-1399">
   <p><strong>示例 #3 <span class="function"><strong>MongoDB\BSON\Serializable::bsonSerialize()</strong></span> returning an associative array for document field</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyDocument&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'document'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MyDocument</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$bson&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">fromPHP</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">toJSON</span><span style="color: #007700">(</span><span style="color: #0000BB">$bson</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
{ &quot;document&quot; : { &quot;foo&quot; : &quot;bar&quot; } }
</pre></div>
   </div>
  </div>

  <div class="example" id="example-1400">
   <p><strong>示例 #4 <span class="function"><strong>MongoDB\BSON\Serializable::bsonSerialize()</strong></span> returning a sequential array for document field</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyArray&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'array'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MyArray</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$bson&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">fromPHP</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">toJSON</span><span style="color: #007700">(</span><span style="color: #0000BB">$bson</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
{ &quot;array&quot; : [ 1, 2, 3 ] }
</pre></div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-mongodb-bson-serializable.bsonserialize-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="mongodb-bson-unserializable.bsonunserialize.html" class="function" rel="rdfs-seeAlso">MongoDB\BSON\Unserializable::bsonUnserialize()</a> - Constructs the object from a BSON array or document</span></li>
   <li class="member"><span class="interfacename"><a href="class.mongodb-bson-persistable.html" class="interfacename">MongoDB\BSON\Persistable</a></span></li>
   <li class="member"><a href="mongodb.persistence.html" class="xref">Persisting Data</a></li>
  </ul>
 </div>


</div></div></div></body></html>